home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / ldb.zip / CBINDER.CPP < prev    next >
C/C++ Source or Header  |  1991-10-18  |  5KB  |  282 lines

  1. /*
  2.  
  3.     cbinder.cpp
  4.     10-18-91
  5.     Copy Binder: Loose Data Binder v1.4
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.  
  11.     PSW / Power SoftWare
  12.     P.O. Box 10072
  13.     McLean, Virginia 22102 8072 USA
  14.  
  15.     John Small
  16.     Voice: (703) 759-3838
  17.     CIS: 73757,2233
  18.  
  19. */
  20.  
  21. #include "cbinder.hpp"
  22.  
  23.  
  24. /*  Copy Binder Primitives  */
  25.  
  26. void CBinder::Dstore(ostream& os, const voiD D)
  27. {
  28.     if (!D)
  29.         return;
  30.     if (sizeofData)  {
  31.         os.write((const char *)D,sizeofData);
  32.         if (!os)
  33.             serror("unable to store CBinder "
  34.                 "node (fixed sized data)");
  35.     }
  36.     else  {
  37.         unsigned i;
  38.         os << (i = (unsigned) strlen((char *)D))
  39.             << endm;
  40.         if (i)
  41.             os.write((char *)D,i);
  42.         if (!os)
  43.             serror("unable to store CBinder "
  44.                 "node (string data)");
  45.     }
  46. }
  47.  
  48. voiD CBinder::Dload(istream& is)
  49. {
  50.     char * D;
  51.  
  52.     if (sizeofData) {
  53.         if ((D = new char[sizeofData])
  54.             != (char *)0)  {
  55.             is.read(D,sizeofData);
  56.             if (!is)  {
  57.                 serror("loading CBinder "
  58.                     "node (fixed sized "
  59.                     "data)");
  60.                 delete D;
  61.                 D = (char *) 0;
  62.             }
  63.         }
  64.         else
  65.             serror("unable to allocate memory "
  66.                 "for CBinder node "
  67.                 "(fixed size data)");
  68.     }
  69.     else  {
  70.         unsigned i;
  71.  
  72.         D = (char *) 0;
  73.         if (!(is >> i >> nextm))
  74.             serror("loading CBinder node "
  75.                 "(string data)");
  76.         else if ((D = new char[i+1])
  77.             != (char *)0)  {
  78.             if (i)  {
  79.                 is.read(D,i);
  80.                 if (!is)  {
  81.                     serror("loading "
  82.                         "CBinder "
  83.                         "node "
  84.                         "(string "
  85.                         "data)");
  86.                     delete D;
  87.                     D = (char *) 0;
  88.                 }
  89.                 else
  90.                     D[i] = '\0';
  91.             }
  92.             else
  93.                 D[i] = '\0';
  94.         }
  95.         else
  96.             serror("unable to allocate memory "
  97.                 "for CBinder node "
  98.                 "(string data)");
  99.     }
  100.  
  101.     return (voiD) D;
  102. }
  103.  
  104. voiD CBinder::Dclone(const voiD D)
  105. {
  106.     voiD cD;
  107.  
  108.     if (!D)
  109.         return voiD0;
  110.     if (sizeofData)  {
  111.         if ((cD = (voiD) new char[sizeofData])
  112.             != voiD0) memcpy(cD,D,sizeofData);
  113.     }
  114.     else if ((cD = (voiD) new char[strlen((char *)D)+1])
  115.         != voiD0) strcpy((char *)cD,(char *)D);
  116.     return cD;
  117.  
  118. }
  119.  
  120. voiD CBinder::Dcopy(voiD D, const voiD S)
  121. {
  122.     if (D && S)  {
  123.         if (sizeofData)
  124.             memcpy(D,S,sizeofData);
  125.         else
  126.             strcpy((char *)D,(char *)S);
  127.         return D;
  128.     }
  129.     return voiD0;
  130. }
  131.  
  132. ostream& CBinder::store(ostream& os)
  133. {    /* Store CBinder sizeofData field then Binder! */
  134.     if (!(os << sizeofData << endm))
  135.         serror("unable to store CBinder");
  136.     else
  137.         SBinder::store(os);
  138.     return os;
  139. }
  140.  
  141. StreamablE CBinder::load(istream& is, StreamablE InstancE)
  142. {
  143.     unsigned sizeofData;
  144.     int newed = 0;
  145.  
  146.     if (!(is >> sizeofData >> nextm))  {
  147.         lserror("unable to load CBinder "
  148.             "header data",ID_CLASS);
  149.         return StreamablE0;
  150.     }
  151.     if (!InstancE)  {
  152.         if ((InstancE = (StreamablE)
  153.             new CBinder(UNIQUE_STREAMABLE))
  154.             == StreamablE0)  {
  155.             lserror("unable to construct "
  156.                 "CBinder",ID_CLASS);
  157.             return StreamablE0;                
  158.         }
  159.         newed = 1;
  160.     }
  161.     ((CBindeR)InstancE)->construct(sizeofData);
  162.     StreamablE ancestoR =  SBinder::load(is,InstancE);
  163.     if (!ancestoR)  {
  164.         // ((CBindeR)InstancE)->destruct();
  165.         if (newed)
  166.             delete InstancE;
  167.         return StreamablE0;
  168.     }
  169.     return ancestoR;
  170. }
  171.  
  172. CBinder::CBinder(unsigned sizeofData,
  173.     unsigned maxNodes, unsigned limit,
  174.     unsigned delta)
  175.     : SBinder(BDR_OK_FREE,maxNodes,
  176.         limit,delta)
  177. {
  178.     construct(sizeofData);
  179.     setID(ID_CLASS);
  180. }
  181.  
  182. voiD CBinder::atInsCLN(unsigned n, const voiD D)
  183. {
  184.     voiD cD;
  185.  
  186.     if (!atIns(n,cD = Dclone(D)))  {
  187.         Dfree(cD);
  188.         return voiD0;
  189.     }
  190.     return cD;
  191. }
  192.  
  193. voiD CBinder::atFreeCPY(unsigned n, voiD D)
  194. {
  195.     return (Dcopy(D,atGet(n))? atFree(n), D : voiD0);
  196. }
  197.  
  198. voiD CBinder::atFreePutCLN(unsigned n, const voiD D)
  199. {
  200.     voiD oldD, cD;
  201.  
  202.     if ((oldD = atGet(n)) == voiD0)
  203.         return voiD0;
  204.     if (!atPut(n,cD = Dclone(D)))  {
  205.         Dfree(cD);
  206.         return voiD0;
  207.     }
  208.     Dfree(oldD);
  209.     return cD;
  210. }
  211.  
  212. CBinder::operator char *()
  213. {
  214.     return (char *) (sizeofData? 0 : current());
  215. }
  216.  
  217. voiD CBinder::pushCLN(const voiD D)
  218. {
  219.     voiD cD;
  220.  
  221.     if (!push(cD = Dclone(D)))  {
  222.         Dfree(cD);
  223.         return voiD0;
  224.     }
  225.     return cD;
  226. }
  227.  
  228. voiD CBinder::popFreeCPY(voiD D)
  229. {
  230.     return (Dcopy(D,atGet(0))? atFree(0), D : voiD0);
  231. }
  232.  
  233. voiD CBinder::insQCLN(const voiD D)
  234. {
  235.     voiD cD;
  236.  
  237.     if (!insQ(cD = Dclone(D)))  {
  238.         Dfree(cD);
  239.         return voiD0;
  240.     }
  241.     return cD;
  242. }
  243.  
  244. voiD CBinder::unQFreeCPY(voiD D)
  245. {
  246.     return (Dcopy(D,bottom())? unQFree(), D : voiD0);
  247. }
  248.  
  249. voiD CBinder::insCLN(const voiD D)
  250. {
  251.     voiD cD;
  252.  
  253.     if (!ins(cD = Dclone(D)))  {
  254.         Dfree(cD);
  255.         return voiD0;
  256.     }
  257.     return cD;
  258. }
  259.  
  260. voiD CBinder::insSortCLN(const voiD D)
  261. {
  262.     voiD cD;
  263.  
  264.     if (!insSort(cD = Dclone(D)))  {
  265.         Dfree(cD);
  266.         return voiD0;
  267.     }
  268.     return cD;
  269. }
  270.  
  271. voiD CBinder::delFreeCPY(voiD D)
  272. {
  273.     return (Dcopy(D,current())? delFree(), D : voiD0);
  274. }
  275.  
  276. voiD CBinder::nextCPY(voiD D)
  277.     { return (D? Dcopy(D,next()) : voiD0); }
  278.  
  279. voiD CBinder::prevCPY(voiD D)
  280.     { return (D? Dcopy(D,prev()) : voiD0); }
  281.  
  282.